<?php
    header("Content-type:text/html;charset=utf-8");
    require('./conn/mysql.php');
   
    $tel = $_POST['tel'];
    $password = $_POST['password'];

    if(!$tel || !$password) die(write(0, '手机号和密码不能为空!'));

    try{

        $sql = "select * from `sys_user` where `user_mobile` = '$tel'";
        $res = mysql::getRow($sql);
        if($res)die(write(0, '手机号已被注册!'));

        $password = md5($password);
        // $sql = "insert into `sys_user` (`user_account`, `user_name`, `user_nickname`, `user_mobile`, `user_pwd`) 
        //         values ('874099938', 'guoye', '郭烨', '15700781723', '张三')";
        $user = genUserName();
        $data = array(
            'user_account' => time().'',
            'user_name' => $user,
            'user_nickname' => $user,
            'user_mobile' => $tel,
            'user_pwd' => $password,
        );
        $res = mysql::exec('sys_user', $data, 'insert');
        if(!$res) die(write(0, '注册失败,用户不存在或密码不正确!'));
        echo write(1, '注册成功');
    }catch(\Throwable $t){
        echo write(0, ''.$t);
    }finally{
         mysql::close();
    }
    function write($code, $msg, $data=[]){
        $rd = array(
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        );
        return json_encode($rd, JSON_UNESCAPED_UNICODE);
    }

    function genUserName() {
        $chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $username = "";
        for ( $i = 0; $i < 6; $i++){
            $username .= $chars[mt_rand(0, strlen($chars))];
        }
        return strtoupper(base_convert(time() - 1420070400, 10, 36)).$username;
    }
?>